package com.huabo.monitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.huabo.monitor.entity.TblTestplan;
import com.huabo.monitor.service.*;
import com.huabo.monitor.util.ConstClass;
import com.huabo.monitor.util.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author：yhr
 * @date:2022-09-13 11:33
 * @description:
 */
@RestController
@Slf4j
@Api(value = "内控测试-测试跟踪", tags = {"内控测试-测试跟踪所有接口"})
@RequestMapping(value = "/nbkz")
public class CsgzController {

    @Resource
    ITblTestplanService testplanService;
    @Autowired
    ITblStaffService iTblStaffService;


    @Autowired
    TblAssessService tblAssessService;

    @Resource
    CsfaService csfaService;

    @Resource
    ITblTestelementService iTblTestelementService;


    @Resource
    CsgzService csgzService;




    @GetMapping(value = "/nkcs/impl/control_test_track_list")
    @ApiOperation("测试跟踪-主页")
    public JsonBean control_test_track_list(
            @ApiParam(name = "pageNumber", value = "pageNumber", defaultValue = "1") @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "plannumber", value = "plannumber") @RequestParam(value = "plannumber", required = false) String plannumber,
            @ApiParam(name = "planname", value = "planname") @RequestParam(value = "planname", required = false) String planname,
            @ApiParam(name = "starttime_min", value = "starttime_min") @RequestParam(value = "starttime_min", required = false) String starttime_min,
            @ApiParam(name = "starttime_max", value = "starttime_max") @RequestParam(value = "starttime_max", required = false) String starttime_max,
            @ApiParam(name = "status", value = "status") @RequestParam(value = "status", required = false) String status,
            @ApiParam(name = "year", value = "year") @RequestParam(value = "year", required = false) String year,

            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        TblTestplan plan = new TblTestplan();
        if (planname != null && planname.length() > 0) {
            plan.setPlanname(planname);
        }
        if (plannumber != null && plannumber.length() > 0) {
            plan.setPlannumber(plannumber);
        }
        if (status != null && status.length() > 0) {
            plan.setPlanstatus(status);
        }
        if (year != null && year.length() > 0) {
            plan.setPlanyear(year);
        }

        TblStaffUtil user = DealUserToken.parseUserToken(token);
//        TblOrganization attribute = (TblOrganization) request.getSession().getAttribute("hbOrgEntity");// 选则的机构
//        TblStaff user = (TblStaff) request.getSession().getAttribute("longUser");

        boolean bool = csfaService.isSJByOrgId(user.getLinkDetp().getOrgid().toString());
        IPage<TblTestplan> iPage;
        if (bool) {
            iPage = csgzService.findAllTrack(plan, pageNumber, starttime_min, starttime_max, user.getCurrentOrg().getOrgid());
        } else {
            iPage = csgzService.findAllnoSjTrack(plan, pageNumber, starttime_min, starttime_max,
                    user.getStaffid().toString());
        }


        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("starttime_min", starttime_min);
        mv.put("starttime_max", starttime_max);
        mv.put("pageBean", iPage);

        mv.put("plan", plan);
        return new JsonBean(200, "success", mv);


    }


    @GetMapping(value = "/csgz/nkcs/plan/detail")
    @ApiOperation("测试跟踪-主页-点击编号")
    public JsonBean nkcs_ctrltest_plan_detail(

            @ApiParam(name = "selectProjectid", value = "testplanid") @RequestParam(value = "selectProjectid") BigDecimal selectProjectid,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        Map<String, Object> mv = new HashMap<>();
        mv.put("test", csfaService.queryOneTestPlanVo(selectProjectid));

        return new JsonBean(200, "success", mv);
    }


    @ApiOperation("测试跟踪-主页-点击任务跟踪")
    @GetMapping(value = "/csgz/csgz_list")
    public JsonBean csgz_list(
            @ApiParam(name = "pageNumber", value = "pageNumber", defaultValue = "1") @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "selectProjectid", value = "testplanid",defaultValue = "211486") @RequestParam(value = "selectProjectid", required = false) BigDecimal selectProjectid,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        IPage<Map<String,Object>> page=new Page<>(pageNumber,ConstClass.DEFAULT_SIZE);

        if (selectProjectid != null ) {
            page = csgzService.findAllnoSjTrack2(selectProjectid, pageNumber);
        }
        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("selectProjectid",selectProjectid);
        mv.put("pageBean",page);
        return new JsonBean(200, "success", mv);

    }

}
